package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.flow.view.impl.BaseFlowView;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.view.IChangeRegisterAccountView;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.presenter.IChangeRegisterAccountPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.ChangeRegisterAccountPresenter;

public class ChangeRegisterAccountFlowView extends BaseFlowView<IChangeRegisterAccountPresenter>
        implements IChangeRegisterAccountView {

    private TextView mPhoneTV;

    private PersonalInfo mPersonInfo;
    private int mFrom;

    public ChangeRegisterAccountFlowView(Activity activity) {
        super(activity);
        mFrom = getActivity().getIntent().getIntExtra(AccountIntentConst.FROM, AccountIntentConst.UNKNOWN);
    }

    public ChangeRegisterAccountFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        int title;
        if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
            title = R.string.register_telephone;
        } else {
            title = R.string.register_email;
        }
        IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(title), ResourcesUtils.getDrawable(R.color.white));
        toolbar.getmMenuTextView().setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
        toolbar.setMenu(R.string.title_modify, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleModifyClick();
            }
        });
    }

    private void handleModifyClick() {
        if (mPersonInfo != null) {
            if (mPersonInfo.isIdNumExist()) {
                Intent intent = Router.fromPath(AccountActivityConst.Path.IDENTITY_NUMBER);
                intent.putExtra(AccountIntentConst.FROM, mFrom);
                ActivityUtils.startActivityForResult(getActivity(), intent);
            } else {
                Intent intent = Router.fromPath(AccountActivityConst.Path.CONFIRM_PASSWORD);
                intent.putExtra(AccountIntentConst.FROM, mFrom);
                ActivityUtils.startActivityForResult(getActivity(), intent);
            }
        } else {
            Intent intent = Router.fromPath(AccountActivityConst.Path.IDENTITY_NUMBER);
            intent.putExtra(AccountIntentConst.FROM, mFrom);
            ActivityUtils.startActivityForResult(getActivity(), intent);
        }
    }

    @Override
    protected IChangeRegisterAccountPresenter createPresenter() {
        return new ChangeRegisterAccountPresenter();
    }

    private void initView() {
        setContentView(R.layout.change_register_account_activity);
        final Activity activity = getActivity();
        mPhoneTV = activity.findViewById(R.id.tv_phone);
        initData();
    }

    private void initData() {
        mPersonInfo = PersonalInfoManager.getInstance().getPersonalInfo();
        if (mPersonInfo != null) {
            if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
                mPhoneTV.setText(mPersonInfo.getUserPhone());
            } else {
                mPhoneTV.setText(mPersonInfo.getEmail());
            }
        }
    }
}
